package com.hao.chatdemo.config;

import com.corundumstudio.socketio.SocketConfig;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.SpringAnnotationScanner;
import com.corundumstudio.socketio.listener.DefaultExceptionListener;
import io.netty.channel.ChannelHandlerContext;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.List;

/**
 * TODO
 *
 * @author 97472
 * @version 1.0
 * @date 2021/7/7 15:48
 */
@Configuration
public class SocketIOConfig {
    @Value("${socketio.hostname}")
    private String hostname;

    @Value("${socketio.port}")
    private Integer port;

    @Value("${socketio.upgradeTimeout}")
    private Integer upgradeTimeout;

    @Value("${socketio.pingInterval}")
    private Integer pingInterval;

    @Value("${socketio.pingTimeout}")
    private Integer pingTimeout;

    /**
     * 配置socketIOServer
     * @return
     */
    @Bean
    public SocketIOServer socketIOServer(){
        com.corundumstudio.socketio.Configuration config = new com.corundumstudio.socketio.Configuration();
        config.setExceptionListener(new DefaultExceptionListener() {

            @Override
            public void onEventException(Exception e, List<Object> args, SocketIOClient client) {
            }

            @Override
            public void onDisconnectException(Exception e, SocketIOClient client) {
            }

            @Override
            public void onConnectException(Exception e, SocketIOClient client) {

            }

            @Override
            public void onPingException(Exception e, SocketIOClient client) {

            }

            @Override
            public boolean exceptionCaught(ChannelHandlerContext ctx, Throwable e) throws Exception {
                if (e instanceof IOException) {
                    if (e.getMessage().contains("Connection reset by peer")) {
                        return true;
                    } else if (e.getMessage().contains("连接超时")) {
                        return true;
                    }
                }
                return true;
            }
        });
        SocketConfig socketConfig = new SocketConfig();
        socketConfig.setSoLinger(-1);
        socketConfig.setReuseAddress(true);
        socketConfig.setTcpKeepAlive(true);
        config.setSocketConfig(socketConfig);
        config.setHostname(hostname);
        config.setPort(port);
        config.setUpgradeTimeout(upgradeTimeout);
        config.setPingInterval(pingInterval);
        config.setPingTimeout(pingTimeout);

        return new SocketIOServer(config);
    }

    /**
     * 用于扫描netty-socketio的注解，比如 @OnConnect、@OnEvent
     */
    @Bean
    public SpringAnnotationScanner springAnnotationScanner(SocketIOServer socketIOServer){
        return new SpringAnnotationScanner(socketIOServer);
    }
}
